<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'

const loginForm = ref({
  username: 'admin',
  password: '123456'
})

const loginRules = () => {
  const validateUsername = (rule, value, callback) => {
    if (value.length < 1) {
      callback(new Error('请输入用户名'))
    } else {
      callback()
    }
  }
  const validatePassword = (rule, value, callback) => {
    if (value.length < 6) {
      callback(new Error('密码必须在6位以上'))
    } else {
      callback()
    }
  }
  return {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      {
        min: 1,
        max: 16,
        message: '用户名必须是1-16位的字符',
        trigger: 'blur'
      },
      { validator: validateUsername, trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      {
        pattern: /^\S{6,15}$/,
        message: '密码必须是6-15位的非空字符',
        trigger: 'blur'
      },
      { validator: validatePassword, trigger: 'blur' }
    ]
  }
}
//显示“登录中”
const loading = ref(false)
//表单
const formRef = ref(null)

//登录逻辑
import { employeeLogin, reqEmployeeInfo } from '@/api/employee.js'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const userStore = useUserStore()
const router = useRouter()

const handleLogin = async () => {
  formRef.value.validate(async (vaild) => {
    if (vaild) {
      console.log('开始登陆')
      const res = await employeeLogin(loginForm.value)
      console.log(res.data.data.token)
      //保存token到store中
      userStore.setToken(res.data.data.token)

      //取出emp的id
      const resEmpInfo = await reqEmployeeInfo(res.data.data.id)
      userStore.setEmpInfo(resEmpInfo.data.data)

      ElMessage.success('员工登录成功')
      router.push('/')
    } else {
      console.log('表单验证失败')
    }
  })
}
</script>

<template>
  <div class="login">
    <div class="login-box">
      <img src="@/assets/images/login-aside.png" />
      <div class="login-form">
        <el-form ref="formRef" :model="loginForm" :rules="loginRules()">
          <div class="login-form-title">
            <img
              src="@/assets/images/logo.png"
              style="width: 139px; height: 42px"
            />
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
              maxlength="16"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              maxlength="15"
            />
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              class="login-btn"
              size="medium"
              type="primary"
              style="width: 100%"
              @click="handleLogin"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #333;
}

.login-box {
  width: 1000px;
  height: 474.38px;
  border-radius: 8px;
  display: flex;
}

.login-box img {
  width: 60%;
  height: auto;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  background: #ffffff;
  width: 40%;
  border-radius: 0px 8px 8px 0px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form .el-form {
  width: 214px;
  height: 307px;
}

.login-form .el-form-item {
  margin-bottom: 30px;
}

.login-form .el-form-item.is-error .el-input__inner {
  border: 0 !important;
  border-bottom: 1px solid #fd7065 !important;
  background: #fff !important;
}

.login-form .input-icon {
  height: 32px;
  width: 18px;
  margin-left: -2px;
}

.login-form .el-input__inner {
  border: 0;
  border-bottom: 1px solid #e9e9e8;
  border-radius: 0;
  font-size: 14px;
  font-weight: 400;
  color: #333333;
  height: 32px;
  line-height: 32px;
}

.login-form .el-input__prefix {
  left: 0;
}

.login-form .el-input--prefix .el-input__inner {
  padding-left: 26px;
}

.login-form .el-input__inner::placeholder {
  color: #aeb5c4;
}

.login-form .el-form-item--medium .el-form-item__content {
  line-height: 32px;
}

.login-form .el-input--medium .el-input__icon {
  line-height: 32px;
}

.login-btn {
  border-radius: 16px;
  padding: 11px 20px !important;
  margin-top: 10px;
  font-weight: 500;
  font-size: 14px;
  border: 0;
  background-color: #a41e27;
}

.login-btn:hover,
.login-btn:focus {
  background: #f44e59;
  color: #ffffff;
}

.login-form-title {
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
}

.login-form-title .title-label {
  font-weight: 500;
  font-size: 20px;
  color: #333333;
  margin-left: 10px;
}
</style>
